<template>
    <div>
      <el-dialog :model-value="props.addInfo?.isShow" :title="props.addInfo?.isAdd ? '添加管理员' : '编辑管理员'" center @close="cancel">
            <el-form :model="adminForm" :rules="rules" ref="formRef">
                <el-form-item label="所属角色" :label-width="formLabelWidth" prop="roleid">
                  <el-select v-model="adminForm.roleid" placeholder="请选择" >
                    <!-- <el-option label="顶级菜单" :value="0" /> -->
                    <el-option 
                      v-for="item in store.getters['role/getroleList']"
                      :key="item.id" 
                      :label="item.rolename" 
                      :value="item.id" />
                  </el-select>
                </el-form-item>
                <el-form-item label="用户名称" :label-width="formLabelWidth" prop="username">
                  <el-input v-model="adminForm.username" autocomplete="off" />
                </el-form-item>
                <el-form-item label="密码" :label-width="formLabelWidth">
                    <el-input v-model="adminForm.password" autocomplete="off" />
                </el-form-item>
                <el-form-item label="状态" :label-width="formLabelWidth">
                    <el-switch v-model="adminForm.status"
                        style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949;" :active-value="1"
                        :inactive-value="2" />
                </el-form-item>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click='cancel'>取消</el-button>
                    <el-button type="primary" @click="confirm">
                        确定
                    </el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>

<script setup lang="ts">
import { defineProps,defineEmits,defineExpose } from 'vue';
import { useStore } from 'vuex';
import { ref,reactive } from 'vue';
import { FormInstance,FormRules } from 'element-plus';
import { useradd,useredit,userinfo } from '@/request/api';
import { ElMessageBox,ElMessage } from 'element-plus';
let store = useStore();
store.dispatch('role/changeRoleAction')
let props = defineProps({
    addInfo:Object
});
let emit = defineEmits(['cancel']);
let formLabelWidth = ref('120px');
let formRef = ref<FormInstance>();
let adminForm = ref({
  roleid: '', //角色编号
  username: "", //管理员名称
  password: "", //密码
  status: 1, //状态   1正常2禁用   number类型number
});
let rules = reactive<FormRules>({
  username: [
    { required: true, message: '请输入用户·名称', trigger: 'blur' },
    { min: 3, max: 11, message: '长度 3 到 11 ', trigger: 'blur' },
  ],
  roleid: [
    { required: true, message: '请输入上级管理员', trigger: 'blur' },
  ],
});
let reset=()=>{
  adminForm.value = reactive({
    roleid: '', //角色编号
    username: "", //管理员名称
    password: "", //密码
    status: 1, //状态   1正常2禁用   number类型number
  });
  formRef.value?.resetFields()
}
let cancel=()=>{
    emit('cancel',false);
    reset()
};
let lookup=(uid:number)=>{
  userinfo({uid}).then((res:any)=>{
    if (res.data.code == 200) {
      adminForm.value = res.data.list;
      adminForm.value.uid = uid;
      adminForm.value.password = '';
    }else{
      ElMessage.error(res.data.msg)
    }
  })
}
let confirm=()=>{
  formRef.value?.validate((val)=>{
    if (val) {
      if (props.addInfo?.isAdd) {
        useradd(adminForm.value).then((res:any)=>{
          if (res.data.code == 200) {
            ElMessage.success(res.data.msg)
            cancel()
            store.dispatch('admin/getUserListAction')
          } else {
            ElMessage.error(res.data.msg)
          }
        })
      } else {
        useredit(adminForm.value).then((res:any)=>{
          if (res.data.code == 200) {
            ElMessage.success(res.data.msg)
            cancel()
            store.dispatch('admin/getUserListAction')
          } else {
            ElMessage.error(res.data.msg)
          }
        })
      }
    }
  })
}
defineExpose({
  lookup
})
</script>

<style scoped>

</style>